package vn.com.demand.lux.criteria;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import vn.com.demand.lux.model.ServiceCardBean;
import vn.com.demand.util.DpUtils;

public class ServiceCardCriteria {

	private Date fromDate;
	private Date toDate;
	private String resourceId;
	private String serviceCard;
	private String contractNumber;
	private String customerCode;
	private String customerName;
	private String address;
	
	@SuppressWarnings("unchecked")
	public List<ServiceCardBean> search() {
		String sql = "select * from demand_lux_servicecard ";
		QueryRunner runner = new QueryRunner(DpUtils.getDataSource());
		try {
			if (null == fromDate && null == toDate && null == resourceId && null == serviceCard && null == contractNumber && null == customerCode) {
				return (List<ServiceCardBean>) runner.query(sql, new BeanListHandler(ServiceCardBean.class));
			} else {
				sql += " where ";
				if (null != fromDate) {
					sql += "date >= '" + fromDate + "' and ";
				}
				if (null != toDate) {
					sql += "date <= '" + toDate + "' and "; 
				}
				if (null != resourceId && resourceId.length() > 0) {
					sql += "resourceid like '%" + resourceId + "%' and ";
				}
				if (null != serviceCard && serviceCard.length() > 0) {
					sql += "cardnumber like '%" + serviceCard + "%' and ";
				}
				if (null != contractNumber && contractNumber.length() > 0) {
					sql += "contractnumber like '%" + contractNumber + "%' and ";
				}
				if (null != customerCode && customerCode.length() > 0) {
					sql += "customercode like '%" + customerCode + "%' and ";
				} 
				if (null != customerName && customerName.length() > 0) {
					sql += "customername like '%" + customerName + "%' and ";
				}
				if (null != address && address.length() > 0) {
					sql += "address like '%" + address + "%' and ";
				}
				sql += " 1 = 1";
				return (List<ServiceCardBean>) runner.query(sql, new BeanListHandler(ServiceCardBean.class));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public Date getFromDate() {
		return fromDate;
	}

	public void setFromDate(Date fromDate) {
		this.fromDate = fromDate;
	}

	public Date getToDate() {
		return toDate;
	}

	public void setToDate(Date toDate) {
		this.toDate = toDate;
	}

	public String getResourceId() {
		return resourceId;
	}

	public void setResourceId(String resourceId) {
		this.resourceId = resourceId;
	}

	public String getServiceCard() {
		return serviceCard;
	}

	public void setServiceCard(String serviceCard) {
		this.serviceCard = serviceCard;
	}

	public String getContractNumber() {
		return contractNumber;
	}

	public void setContractNumber(String contractNumber) {
		this.contractNumber = contractNumber;
	}

	public String getCustomerCode() {
		return customerCode;
	}

	public void setCustomerCode(String customerCode) {
		this.customerCode = customerCode;
	}

}
